package utils
{
    
    import flash.events.*;
    import flash.media.*;
    import flash.net.*;
    import flash.utils.Dictionary;
    
    import memory.*;

    public class MusicManager
    {
        private var sounds:Dictionary=new Dictionary();
        private var soundChannels:Dictionary=new Dictionary();
        private var currentMusic:String;
        private static var instance:MusicManager;

        public function MusicManager()
        {
            if (instance == null)
            {
                instance = this;
            }
            else
            {
                throw new Error("MusicManager不能实例化");
            }
        }

        public static function getInstance() : MusicManager
        {
            if (instance == null)
            {
                return new MusicManager;
            }
            return instance;
        }
        /**
        * 设置指定ID的声音的音量
        */ 
        public function setVolume(id:String,val:Number):void
        {
        	if(val<0 || val>1) return;
        	var channel:SoundChannel=soundChannels[id];
        	if (channel != null && channel.soundTransform!=null)
        	{
        		channel.soundTransform.volume=val;
        	}
        }
        /**
        * 停止音乐
        */ 
        public function soundStop(id:String) : Number
        {
            if (soundChannels[id] != null)
            {
                soundChannels[id].stop();
                return soundChannels[id].position;
            }
            return 0;
        }
        /**
        * 注册音乐
        */ 
        public function regedit(id:String,sound:Sound) : void
        {
        	if(sounds[id]!=null) throw new Error("音乐的ID已经被注册，请用其他ID");
        	sounds[id]=sound;
        }
        /**
        * 播放指定的音乐
        */ 
        public function playMusic(id:String,start:Number,loops:int):void
        {
        	soundChannels[id] = sounds[id].play( start,loops);
        }
        /**
        * 销毁指定的音乐
        */ 
        public function dispose(id:String) : void
        {
            soundStop(id);
            soundChannels[id] = null;
            sounds[id] = null;
        }
    }
}
